package com.itheima.controller;

import com.itheima.pojo.Achievement;
import com.itheima.service.AchievementService;
import com.itheima.utils.Code;
import com.itheima.utils.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author huangbin
 * @date 2022/6/6 19:38
 */
@RestController
@RequestMapping("/achievement")
public class AchievementController {
    @Autowired
    private AchievementService achievementService;

    @PostMapping
    @ResultMap("ResultAchievementMapper")
    Result add(@RequestBody Achievement achievement) {
        Integer result = achievementService.add(achievement);
        Integer code = result > 0 ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = result > 0 ? "添加成功" : "添加失败";
        return new Result(code,msg,null);
    }

    @GetMapping("/sumPrice")
    @ResultMap("ResultAchievementMapper")
    Result sumMPrice() {
        String result = achievementService.sumMPrice().toString();

        Integer code =  result.length() != 0? Code.GET_OK : Code.GET_ERR;
        String msg = result.length() != 0 ? "查询成功" : "查询失败";
        return new Result(code,msg,result);

    }

    @GetMapping(value = "/IdMPrice")
    @ResultMap("ResultAchievementMapper")
    Result IdMPrice() {
        List<Map<String, Object>> result = achievementService.IdMPrice();

        Integer code =  result != null? Code.GET_OK : Code.GET_ERR;
        String msg =result != null ? "查询成功" : "查询失败";
        return new Result(code,msg,result);

    }
}
